﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Domain.Query.DishDeliveryModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.DishDeliveryModule.DataTransferObject;
using Resto.CostControl.Core.Enums;
using Resto.CostControl.Core.Domain.Model;

namespace Resto.CostControl.Core.Facade.DishDeliveryModule.FacadeObject
{
    /// <summary>
    /// 菜肴发货单FO
    /// </summary>
    public class DishDeliveryFO : IApplicationFacadeObject
    {
        #region 私有属性

        private DishDeliveryDQO _DishDeliveryDQO;
        private DishDeliveryDQO DishDeliveryDQO
        {
            get
            {
                if (_DishDeliveryDQO == null)
                    _DishDeliveryDQO = ObjectCreator.Create<DishDeliveryDQO>();

                return _DishDeliveryDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddDishDelivery(DishDeliveryDTO deliveryDTO, DishDeliveryState state)
        {
            DishDelivery delivery = deliveryDTO.ToDishDelivery();
            delivery.UpdateState(state);

            delivery.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            deliveryDTO.ID = delivery.ID;
        }

        public virtual void UpdateDishDelivery(DishDeliveryDTO deliveryDTO, DishDeliveryState state)
        {
            DishDelivery delivery = DishDeliveryDQO.GetDishDelivery(deliveryDTO.ID);
            delivery.UpdateState(state);

            deliveryDTO.CopyToDishDelivery(delivery);

            delivery.Update();

            if (state == DishDeliveryState.Sign)
            { 
                //保存库存变更记录
                foreach (DishDeliveryItem item in delivery.Items)                
                    MaterialStorageHistory.CreateStorageHistory(item);
                
            }

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual void DeleteDishDelivery(string id)
        {
            DishDelivery supplierContract = DishDeliveryDQO.GetDishDelivery(id);

            supplierContract.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

        }

        public virtual DishDeliveryDTO GetDishDeliveryDTO(string id)
        {
            DishDelivery supplierContract = DishDeliveryDQO.GetDishDelivery(id);

            return new DishDeliveryDTO(supplierContract);
        }

        #endregion

       
    }
 
}
